package cn.anecansaitin.firecrafting.client.integration.jei;

import cn.anecansaitin.firecrafting.ModConstant;
import cn.anecansaitin.firecrafting.api.common.crafting.IFireRecipe;
import cn.anecansaitin.firecrafting.client.screen.FireInterfaceScreen;
import cn.anecansaitin.firecrafting.common.crafting.FireItemRecipe;
import cn.anecansaitin.firecrafting.common.crafting.ModRecipeTypes;
import cn.anecansaitin.firecrafting.common.item.ModItems;
import com.google.common.collect.Lists;
import mezz.jei.api.IModPlugin;
import mezz.jei.api.JeiPlugin;
import mezz.jei.api.helpers.IGuiHelper;
import mezz.jei.api.recipe.RecipeType;
import mezz.jei.api.registration.*;
import mezz.jei.api.runtime.IJeiRuntime;
import net.minecraft.client.Minecraft;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import org.jetbrains.annotations.NotNull;

import java.util.List;
import java.util.Objects;

//JEI拓展的主类，通过@JeiPlugin来注册
@JeiPlugin
public class JEIFireCraftingPlugin implements IModPlugin {
    private static final ResourceLocation ID = new ResourceLocation(ModConstant.MOD_ID, "jei_plugin");
    public static final RecipeType<IFireRecipe> FIRE_RECIPE = RecipeType.create(ModConstant.MOD_ID, IFireRecipe.TYPE_ID, IFireRecipe.class);

    @Override
    public @NotNull ResourceLocation getPluginUid() {
        return ID;
    }

    //注册配方对应Categories
    @Override
    public void registerCategories(@NotNull IRecipeCategoryRegistration registration) {
        IGuiHelper helper = registration.getJeiHelpers().getGuiHelper();
        registration.addRecipeCategories(new FireCategories(helper));
    }

    //注册配方
    @Override
    public void registerRecipes(@NotNull IRecipeRegistration registration) {
        List<IFireRecipe> recipes = Lists.newArrayList(Objects.requireNonNull(Minecraft.getInstance().level).getRecipeManager().getAllRecipesFor(ModRecipeTypes.FIRE_TYPE.get()));
        for (int i = 0; i < recipes.size(); i++) {
            IFireRecipe recipe = recipes.get(i);
            if (recipe instanceof FireItemRecipe) {
                if (((FireItemRecipe) recipe).getOutputs().get(0).getItem() == ModItems.COLOURFUL_FIRE.get()) {
                    recipes.remove(i);
                    break;
                }
            }
        }
        registration.addRecipes(FIRE_RECIPE, recipes);
    }

    //对着物品按U可查看相关配方
    @Override
    public void registerRecipeCatalysts(IRecipeCatalystRegistration registration) {
        registration.addRecipeCatalyst(new ItemStack(ModItems.FIRE_INTERFACE.get()), FIRE_RECIPE);
        registration.addRecipeCatalyst(new ItemStack(Items.FLINT_AND_STEEL), FIRE_RECIPE);
    }

    //在gui内添加查看所有配方的区域
    @Override
    public void registerGuiHandlers(IGuiHandlerRegistration registration) {
        registration.addRecipeClickArea(FireInterfaceScreen.class, 79, 64, 17, 15, FIRE_RECIPE);
    }

    //添加物品转移按钮
    @Override
    public void registerRecipeTransferHandlers(IRecipeTransferRegistration registration) {
        registration.addRecipeTransferHandler(new FireCraftingRecipeTransferInfo());
    }

}
